////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//
//
// This file is part of the Microsoft .NET Micro Framework Porting Kit Code Samples and is unsupported.
// Copyright (C) Microsoft Corporation. All rights reserved. Use of this sample source code is subject to
// the terms of the Microsoft license agreement under which you licensed this sample source code.
//
// THIS SAMPLE CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED,
// INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
//
//
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

#ifndef _STNAND0x_H_1
#define _STNAND0x_H_1   1

#define FLASH_START_BLOCK						(32)
#define FLASH_START_SECTOR						(FLASH_START_BLOCK*FLASH_SECTOR_PER_BLOCK)

#define FLASH_MANUFACTURER_CODE                 0x20

#define FLASH_BASE_ADDRESS_ADJUSTED				0

#define FLASH_SECTOR_SIZE                       0x800
#define FLASH_BLOCK_SIZE                        0x20000

#define FLASH_SECTOR_PER_BLOCK                  (FLASH_BLOCK_SIZE/FLASH_SECTOR_SIZE)
#define FLASH_BLOCK_ERASE_TYPICAL_TIME_USEC     1500000
#define FLASH_SECTOR_WRITE_TYPICAL_TIME_USEC    200
#define FLASH_BLOCK_ERASE_MAX_TIME_USEC         2000000
#define FLASH_SECTOR_WRITE_MAX_TIME_USEC        700
#define FLASH_SPARE_SIZE						64

// ST NAND 01/02
#define FLASH_BLKS_1GBIT						(1024-FLASH_START_BLOCK)
#define FLASH_BLKS_2GBIT					 	(2048-FLASH_START_BLOCK)

//--//

/// Nand flash chip status codes
#define STATUS_NO_WP                    		(1 << 7)
#define STATUS_READY                    		(1 << 6)
#define STATUS_ERROR                    		(1 << 0)


struct STNAND0x_BS_Driver
{
    typedef UINT8 CHIP_WORD;

	//--//

	char *devname;

	//--//

    static BOOL ChipInitialize( void* context );

    static BOOL ChipUnInitialize( void* context );

    static const BlockDeviceInfo* GetDeviceInfo( void* context );

    static BOOL Read( void* context, ByteAddress Address, UINT32 NumBytes, BYTE * pSectorBuff );

    static BOOL Write( void* context, ByteAddress Address, UINT32 NumBytes, BYTE * pSectorBuff, BOOL ReadModifyWrite );

    static BOOL Memset( void* context, ByteAddress Address, UINT8 Data, UINT32 NumBytes );

    static BOOL GetSectorMetadata(void* context, ByteAddress SectorStart, SectorMetadata* pSectorMetadata);

    static BOOL SetSectorMetadata(void* context, ByteAddress SectorStart, SectorMetadata* pSectorMetadata);

    static BOOL IsBlockErased( void* context, ByteAddress address, UINT32 blockLength );

    static BOOL EraseBlock( void* context, ByteAddress Sector );

    static void SetPowerState( void* context, UINT32 State );

    static UINT32 MaxSectorWrite_uSec( void* context );

    static UINT32 MaxBlockErase_uSec( void* context );

//--//
    static BOOL ChipReadOnly( void* context, BOOL On, UINT32 ProtectionKey );

    static BOOL ReadProductID( void* context, FLASH_WORD& ManufacturerCode, FLASH_WORD& DeviceCode );
private:

	static BOOL WriteInternal( void* context, ByteAddress Address, UINT32 NumBytes, BYTE * pSectorBuff, BOOL ReadModifyWrite, BOOL fill );


};

#endif // _STNAND0x_H_1

